/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import helper.NewHibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import pojo.*;
/**
 *
 * @author QTuan
 */
public class DAOPersonnel {
    private Session session;
    public DAOPersonnel()
    {
        SessionFactory factory = NewHibernateUtil.getSessionFactory();
        this.session = factory.getCurrentSession();
    }
    public boolean DangNhap(String Taikhoan,String Pass)
    {
        try{
            session.getTransaction().begin();
            String sql = "from Personnel where per_account = ? and per_pass = ?";
            Query query = session.createQuery(sql);
            query.setString(0, Taikhoan);
            query.setString(1,Pass);
            Personnel result = (Personnel)query.uniqueResult();
            session.flush();
            session.getTransaction().commit();
            return true;
        }catch(Exception e)
        {
            if(session.getTransaction().isActive())
            {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return false;
    }
    public PersonnelDetail getDetailAccount(String account)
    {
        try{
            session.getTransaction().begin();
            String sql = "from PersonnelDetail where per_account = ?";
            Query query = session.createQuery(sql);
            query.setString(0, account);
            PersonnelDetail result = (PersonnelDetail)query.uniqueResult();
            session.flush();
            session.getTransaction().commit();
            return result;
        }catch(Exception e)
        {
            if(session.getTransaction().isActive())
            {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
    public Personnel getAccount(String account)
    {
        try{
            session.getTransaction().begin();
            String sql = "from Personnel where per_account = ?";
            Query query = session.createQuery(sql);
            query.setString(0, account);
            Personnel result = (Personnel)query.uniqueResult();
            session.flush();
            session.getTransaction().commit();
            return result;
        }catch(Exception e)
        {
            if(session.getTransaction().isActive())
            {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
}
